﻿/*========================== 
 * @author 郑金泉
 * @desc IO实用工具
 * ========================= */
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;

namespace Obsidian.Utils
{
    public class IOUtil
    {
        /// <summary>
        /// 读取文件中的文本
        /// </summary>
        /// <param name="filePath">文件地址</param>
        /// <param name="encodingName">编码名称</param>
        /// <returns>文本</returns>
        public static string ReadTextFromFile(string filePath,string encodingName)
        {
            string resultText = "";
            try
            {
                Encoding encoding = Encoding.GetEncoding(encodingName);
                StreamReader sr = new StreamReader(filePath, encoding);
                resultText = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
            }
            catch (Exception e)
            {
                throw e;
            }
            return resultText;
        }

        public static string StreamToString(Stream stream)
        {
            string streamString = null;
            Byte[] byteArr = new Byte[stream.Length];
            stream.Read(byteArr, 0, Convert.ToInt32(stream.Length));
            streamString = Encoding.UTF8.GetString(byteArr);
            return streamString;
        }

        /// <summary>
        /// 向文件追加文本
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="text">追加的文本</param>
        public static void FileAddendText(string path,string text)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path, true, Encoding.Default);
                sw.Write(text);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void WriterTextToFile(string file, bool append, string text)
        {
            StreamWriter sw = new StreamWriter(file, append, Encoding.GetEncoding("utf-8"));
            try
            {
                sw.Write(text);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                sw.Close();
                sw.Dispose();
            }
        }


        public static bool Zip(string zipFilePath, string[] files)
        {
            return Zip(zipFilePath, files, 9);
        }

        public static bool Zip(string zipFilePath, string[] files, int level)
        {
            try
            {
                FileStream zipstream = new FileStream(zipFilePath, FileMode.Create);
                using (ZipOutputStream s = new ZipOutputStream(zipstream))
                {
                    s.SetLevel(level);
                    byte[] buf = new byte[4096];
                    foreach (string fileName in files)
                    {
                        FileInfo file = new FileInfo(fileName);
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file.FullName));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream zfs = File.OpenRead(file.FullName))
                        {
                            int sourcebytes;
                            do
                            {
                                sourcebytes = zfs.Read(buf, 0, buf.Length);
                                s.Write(buf, 0, sourcebytes);
                            } while (sourcebytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                return true;
            }
        }

        public static byte[] ReadFileBytes(string path)
        {
            FileStream stream = new FileInfo(path).OpenRead();
            Byte[] buffer = new Byte[stream.Length];
            //从流中读取字节块并将该数据写入给定缓冲区buffer中
            stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
            stream.Close();
            return buffer;
        }

    }
}
